/*!
 * Copyright (c)2008-2009, stauren.net All rights reserved.
 * @file anim.js
 * @module Anim
 * @author <a href="mailto:stauren@stauren.net">stauren</a>
 * @revision:
 */

if (typeof __ != 'undefined' && !__.Anim) (function() {
  __.log('Anim: Def begin.');

  var _SELF = __,
  _STRING = 'string',
  _TRUE = true,
  _FALSE = false,
  _NULL = null,
  _FILTER = 'filter',
  _OPACITY = 'opacity';

  _SELF.add('Anim', (function() {
    var Anim = function(el, attr, ftobj) {
      var that = this;
      if (typeof ftobj != 'object') {
        ftobj = {to : ftobj};
      }
      this.el = typeof el == _STRING ? _SELF.Dom.f(el) : el;
      this.attr = attr;
      this.from = 'from' in ftobj ? ftobj.from : _FALSE;
      this.to = ftobj.to;
      this.unit = 'unit' in ftobj ? ftobj.unit : '';
      this.lasttime = 'lasttime' in ftobj ? ftobj.lasttime : 300;
      this.interval = 'interval' in ftobj ? ftobj.interval : 15;
      this.max = 'max' in ftobj ? ftobj.max : this.lasttime * 2 / this.interval;
      //this.isStyle = 'isStyle' in ftobj ? ftobj.isStyle : true;
      this.onOver = 'onOver' in ftobj ? ftobj.onOver : _SELF.Lang.EF;
      this.paused = _FALSE;
      this.iid = _NULL;
      this.running = _FALSE;
      this._set = (function() {
        var el = ftobj.isStyle === false ? that.el : that.el.style;
        return attr == _OPACITY && typeof el[_FILTER] == _STRING ? function(v) {
          el[_FILTER] = 'alpha(' + _OPACITY + '=' + v * 100 + ')';
        } : function(v) {
          el[attr] = v + that.unit;
        };
      }());
      this._get = (function() {
        var el = ftobj.isStyle === false ? that.el : that.el.style;
        return attr == _OPACITY && typeof el[_FILTER] == _STRING ? function() {
          var val = 100;
          try {
            val = that.el.filters['DXImageTransform.Microsoft.Alpha'][_OPACITY];
          } catch(e) {
            try {
                val = that.el.filters('alpha')[_OPACITY];
            } catch(err) {}
          }
          return val / 100;
        } : function() {
          return parseFloat(el[attr]) || 0;
        };
      }());
    };
    Anim.prototype = {
      run : function() {
        if (this.running) {
          return;
        }
        this.running = _TRUE;

        //var el = this.isStyle ? this.el.style : this.el, at = this.attr,
        var oldF;
        if (!this.paused) {
          oldF = this.from;
          this.from = oldF === _FALSE? this._get() : oldF;
          this.piece = (this.to - this.from) * (this.interval+1) / this.lasttime;
          //el[at] = this.from + this.piece + this.unit;
          this._set(this.from + this.piece);
          this.from = oldF;
        }
        if (this.piece === 0) {
          this.running = _FALSE;
          return;
        }
        this.iid = this._begin();
      },
      _begin : function() {
        var that = this,// el = this.isStyle ? this.el.style : this.el,
        fn = (function() {
          return that.piece > 0 ? function() {
            //var oldV = parseInt(el[that.attr], 10) || 0;
            var oldV = that._get();
            if (that.count++ >= that.max || (oldV+that.piece >= that.to)) {
              that._stop();
              //el[that.attr] = that.to + that.unit;
              that._set(that.to);
              that.onOver && that.onOver();
              return;
            }
            //el[that.attr] = oldV + that.piece + that.unit;
            that._set(oldV + that.piece);
          } : function() {
            var oldV = that._get();
            if (that.count++ >= that.max || (oldV+that.piece <= that.to)) {
              //el[that.attr] = that.to + that.unit;
              that._set(that.to);
              that._stop();
              that.onOver && that.onOver();
              return;
            }
            //el[that.attr] = oldV + that.piece + that.unit;
            that._set(oldV + that.piece);
          }
        }());
        this.count = 0;
        return setInterval(fn, that.interval);
      },
      pause : function() {
        this.paused = _TRUE;
        this.running = _FALSE;
        clearInterval(this.iid);
      },
      _stop : function() {
        this.running = _FALSE;
        clearInterval(this.iid);
      },
      stop : function() {
        if (this.running) {
          this._stop();
        }
      }
    };
    return Anim;
  }()));

  __.log('Anim: Def end.');
}());
